package com.golddaymicromall.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.golddaymicromall.activity.R;
import com.golddaymicromall.activity.salemanager.SaleOrderDetailActivity;
import com.golddaymicromall.bean.OrderInfo;
import com.golddaymicromall.util.IsNetUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/3/3 0003.
 */
public class SaleManagerAdapter extends BaseAdapter {

    private List<OrderInfo> list = new ArrayList<OrderInfo>();
    private Context ctx;
    LayoutInflater layoutinflater;
    private IsNetUtils isnet;

    public SaleManagerAdapter(List list,Context ctx){
        this.list = list;
        this.ctx = ctx;
        isnet = new IsNetUtils(ctx);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return getItem(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        View view = convertView;
        if(view == null){
            viewHolder = new ViewHolder();
            layoutinflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = layoutinflater.inflate(R.layout.item_sale, null);
            viewHolder.mOrderId = (TextView) view.findViewById(R.id.sale_orderId);
            viewHolder.mOrderType = (TextView) view.findViewById(R.id.sale_orderType);
            viewHolder.mOrderStatus = (TextView) view.findViewById(R.id.sale_orderStatus);
            view.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) view.getTag();
        }
        if(list.size() > 0){
            viewHolder.mOrderId.setText(list.get(position).getOrderName());
            switch (list.get(position).getOrderStatus()){
                case "0":
                    viewHolder.mOrderStatus.setText("未付款");
                    break;
                case "1":
                    viewHolder.mOrderStatus.setText("已付款");
                    break;
                case "3":
                    viewHolder.mOrderStatus.setText("删除");
                    break;
                default:
                    viewHolder.mOrderStatus.setText("");
                    break;
            }
            switch (list.get(position).getOrderType()){
                case "1":
                    viewHolder.mOrderType.setText("首次进货");
                    break;
                case "2":
                    viewHolder.mOrderType.setText("加盟类型变更");
                    break;
                case "3":
                    viewHolder.mOrderType.setText("二次进货");
                    break;
                case "4":
                    viewHolder.mOrderType.setText("赠送订单");
                    break;
                case "12":
                    viewHolder.mOrderType.setText("预购货款");
                    break;
                case "3000":
                    viewHolder.mOrderType.setText("积分兑换");
                    break;
                default:
                    viewHolder.mOrderType.setText("");
                    break;
            }
            viewHolder.mOrderId.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!isnet.IsNet()) {
                        Toast.makeText(ctx, "网络漫游中请查看是否有网", Toast.LENGTH_SHORT).show();
                    }else {
                        if ("4".equals(list.get(position).getOrderType())) {
                            Toast.makeText(ctx, "优惠单暂无数据", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        Intent intent2OrderDetail = new Intent(ctx, SaleOrderDetailActivity.class);
                        intent2OrderDetail.putExtra("orderId", list.get(position).getId());
                        intent2OrderDetail.putExtra("orderName", list.get(position).getOrderName());
                        intent2OrderDetail.putExtra("orderType", list.get(position).getOrderType());
                        intent2OrderDetail.putExtra("orderClass", list.get(position).getOrderClass());
                        ctx.startActivity(intent2OrderDetail);
                    }
                }
            });
        }else{
            Toast.makeText(ctx, "没有请求到数据,请稍后重试", Toast.LENGTH_SHORT).show();
        }
        return view;
    }

    private class ViewHolder{
        TextView mOrderId;
        TextView mOrderType;
        TextView mOrderStatus;
    }
}
